package com.xzy.urp.controller;


import cn.hutool.core.bean.BeanUtil;
import com.xzy.urp.common.exception.BusinessException;
import com.xzy.urp.common.result.R;
import com.xzy.urp.common.result.ResponseEnum;
import com.xzy.urp.pojo.entity.StudentInfo;
import com.xzy.urp.pojo.vo.CourseTimeVO;
import com.xzy.urp.pojo.vo.StudentInfoVO;
import com.xzy.urp.pojo.vo.TeacherInfoVO;
import com.xzy.urp.service.StudentInfoService;
import com.xzy.urp.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xzy
 * @since 2022-02-17
 */
@RestController
@RequestMapping("/admin/studentInfo")
@Api(tags = "学生信息管理接口")
public class StudentInfoController {
//个人信息查询
//本学期成绩
//不及格成绩
//所有成绩
//学籍信息录入

    private final StudentInfoService studentInfoService;

    @Autowired

    public StudentInfoController(StudentInfoService studentInfoService) {
        this.studentInfoService = studentInfoService;
    }



    @ApiOperation("学籍信息录入（管理员权限）")
    @PostMapping("/add")
    public R SInfoEntry(@ApiParam("教师信息视图")
                            @RequestBody StudentInfoVO studentInfoVO) {
        if (studentInfoService.addSInfo(studentInfoVO)){
            return R.ok().message("录入成功");
        }
        return R.error().message("录入失败");
    }
    @ApiOperation("学生个人信息查询")
    @GetMapping("/getInfo")
    public R SInfoQuery(HttpServletRequest request) {
        String token = request.getHeader("token");  // 获取令牌(前端将token放入请求头中)
        Long userId = JwtUtils.getUserId(token);
        StudentInfo studentInfo = studentInfoService.getInfoById(userId);
        StudentInfoVO studentInfoVO = new StudentInfoVO();
        BeanUtil.copyProperties(studentInfo,studentInfoVO);
        return R.ok().message("获取成功").data("studentInfoVO",studentInfoVO);
    }
    @ApiOperation("本学期成绩查询")
    public R semesterGrade() {
        return R.ok();
    }
    @ApiOperation("不及格成绩查询")
    public R failGrade() {
        return R.ok();
    }
    @ApiOperation("所有成绩查询查询")
    public R allGrade() {
        return R.ok();
    }

    @ApiOperation("个人课表")
    @GetMapping("/listTimeTable/{year}")
    public R sSchedule(HttpServletRequest request,
                       @ApiParam("学年信息") @PathVariable Integer year) {
        String token = request.getHeader("token");  // 获取令牌(前端将token放入请求头中)
        Long userId = JwtUtils.getUserId(token);
        List<CourseTimeVO> courseTimes = studentInfoService.getCTimeById(userId, year);
        return R.ok().message("获取成功").data("list",courseTimes);
    }

    @ApiOperation(value = "excel表批量导入学生信息（管理员权限）")
    @PostMapping("/Excel/import")
    public R teacherExcel(@ApiParam(value = "Excel数据字典文件",required = true)
                          @RequestParam("file") MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            studentInfoService.importData(inputStream);
            return R.ok().message("数据导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException((ResponseEnum.UPLOAD_ERROR),e);
        }
    }

}

